﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DAL;
using BAL;

namespace UI
{
    public partial class SiteMaster : MasterPage
    {
        NhaSXBus nsx = new NhaSXBus();
        SanPhamBus sp = new SanPhamBus();
        private void loadSPDN()
        {
             DataTable dt =  nsx.LoadSPDacBien();
             lbspdb.Text = dt.Rows[0]["TenSp"].ToString();
             imgspdb.ImageUrl = "images/" + dt.Rows[0]["Image1"].ToString();
             lbgia.Text = dt.Rows[0]["DonGia"].ToString(); 
        }
        private void loadSPMoi()
        {

            DataTable ddt = DBConnection.getALL("sp_QuangCaobyMucPhiMax");
            imgquangcao.ImageUrl = "images/" + ddt.Rows[0]["LinkImage"].ToString();
            lbLinkQC.Text = "<a href=\"http://" + ddt.Rows[0]["LinkUrl"].ToString() + "\">";
            imgQC1.ImageUrl = "images/" + ddt.Rows[1]["LinkImage"].ToString();
            lbLinkQC1.Text = "<a href=\"http://" + ddt.Rows[1]["LinkUrl"].ToString() + "\">";


            DataTable dt = sp.getAllSanPham();
            lbtenspm.Text = dt.Rows[0]["TenSp"].ToString();
            imgspm.ImageUrl = "images/" + dt.Rows[0]["Image1"].ToString();
            lbgiaspm.Text = dt.Rows[0]["DonGia"].ToString();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["GioHang"] == null)
                TaoGioHang();

            XuatThongTinGioHang();
            lbmenu.Text = nsx.LoadMenu();
            loadSPDN();
            loadSPMoi();
        }
        private void TaoGioHang()
        {
            DataTable gioHang = new DataTable();
            gioHang.Columns.Add("maSP", typeof(string));
            gioHang.Columns.Add("hinh", typeof(string));
            gioHang.Columns.Add("tenhang", typeof(string));
            gioHang.Columns.Add("dongia", typeof(int));
            gioHang.Columns.Add("soluong", typeof(int));

            Session["GioHang"] = gioHang;
        }
        private void XuatThongTinGioHang()
        {
            DataTable dt = (DataTable)Session["GioHang"];
            int soSP = 0;
            int soTien = 0;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                soTien = soTien + int.Parse(dt.Rows[i]["dongia"].ToString()) * int.Parse(dt.Rows[i]["soluong"].ToString());
                soSP = soSP + int.Parse(dt.Rows[i]["soluong"].ToString());
            }
            lbtensp.Text = "" + soSP;
            lbtongtien.Text = "" + soTien;
        }
    }
}